package com.opticon.vaadinR.ui.panel;

import com.google.common.base.Joiner;
import com.google.common.io.Files;
import com.vaadin.ui.*;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * This class is a
 */
public class SimpleTextTab extends VerticalLayout {
    private static Logger logger = Logger.getLogger(SimpleTextTab.class);

    private String iTitle = "SimpleTextTab";
    public String iContent = "no content loaded";
    public TextArea iTextArea = null;
    private File iFile = null;

    public SimpleTextTab(File aFile) {
        iFile = aFile;
        initialize();
    }

    public SimpleTextTab(String aContent) {
        iContent = aContent;
        initialize();
    }

    public Component getComponent() {
        return this;
    }

    public void initialize() {

        setCaption();

        try {
            iTextArea = new TextArea();

            if (iFile != null) {
                List<String> lines = Files.readLines(iFile, Charset.defaultCharset());
                iContent = Joiner.on("\n").join(lines);

            }
            iTextArea.setValue(iContent);
            iTextArea.setSizeFull();
            iTextArea.setRows(30);
            this.addComponent(iTextArea);

            this.requestRepaintAll();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * Sets the field's caption <code>String</code>. Caption is the visible
     * name of the field. This method will trigger a
     * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
     * RepaintRequestEvent}.
     */
    public void setCaption() {
        super.setCaption(iTitle);
    }
}
